package com.gewu.pm.ui.fragment.terminal;

import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.ui.adapter.terminal.TerminalOrderAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 终端详情 - 订单 Fragment
 */
public final class OrderFragment extends AppFragment<com.gewu.pm.app.AppActivity>
        implements BaseAdapter.OnItemClickListener {

    private TextView mRecordCountView;
    private TextView mDateRangeView;
    private RecyclerView mRecyclerView;
    private TerminalOrderAdapter mAdapter;

    public static OrderFragment newInstance() {
        return new OrderFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.terminal_order_fragment;
    }

    @Override
    protected void initView() {
        mRecordCountView = findViewById(R.id.tv_record_count);
        mDateRangeView = findViewById(R.id.tv_date_range);
        mRecyclerView = findViewById(R.id.rv_order_list);

        // 设置 RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new TerminalOrderAdapter(getContext());
        
        // 必须在 setAdapter 之前设置监听器
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        // 点击日期范围
        mDateRangeView.setOnClickListener(v -> {
            // TODO: 打开日期选择器
            toast("选择日期范围");
        });
    }

    @Override
    protected void initData() {
        // TODO: 加载订单数据
        List<String> testData = new ArrayList<>();
        testData.add("订单1");
        testData.add("订单2");
        testData.add("订单3");
        testData.add("订单4");
        mAdapter.setData(testData);
        updateRecordCount();
    }

    /**
     * 更新记录数
     */
    private void updateRecordCount() {
        int count = mAdapter.getCount();
        mRecordCountView.setText("共" + count + "条记录");
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, android.view.View itemView, int position) {
        String orderNo = mAdapter.getItem(position);
        toast("点击订单：" + orderNo);
        // TODO: 打开订单详情
    }
}

